﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using Microsoft.Extensions.DependencyInjection;
using QModels;
using QServices;
using QServices.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QControlCenter.ViewModel
{
    public partial class DeviceManagerViewModel: QBaseVM
    {

        [ObservableProperty]
        private string iD;

        [ObservableProperty]
        private string? region;

        [ObservableProperty]
        private decimal temperatureIndoor_1;
        [ObservableProperty]
        private int temperatureIndoor_status_1;
        [ObservableProperty]
        private bool temperatureIndoor_open_1;

        [ObservableProperty]
        private decimal temperatureIndoor_2;
        [ObservableProperty]
        private int temperatureIndoor_status_2;
        [ObservableProperty]
        private bool temperatureIndoor_open_2;

        [ObservableProperty]
        private decimal temperatureIndoor_3;
        [ObservableProperty]
        private int temperatureIndoor_status_3;
        [ObservableProperty]
        private bool temperatureIndoor_open_3;


        [ObservableProperty]
        private decimal temperatureOutdoor_1;
        [ObservableProperty]
        private int temperatureOutdoor_status_1;
        [ObservableProperty]
        private bool temperatureOutdoor_open_1;

        [ObservableProperty]
        private decimal co2_1;
        [ObservableProperty]
        private int co2_status_1 ;
        [ObservableProperty]
        private bool co2_open_1;
        [ObservableProperty]
        private decimal o3_1;
        [ObservableProperty]
        private int o3_status_1;
        [ObservableProperty]
        private bool o3_open_1;


        [ObservableProperty]
        private int daFengJi_status_1;
        [ObservableProperty]
        private bool daFengJi_open_1;

        [ObservableProperty]
        private int daFengJi_status_2;
        [ObservableProperty]
        private bool daFengJi_open_2;

        [ObservableProperty]
        private int daFengJi_status_3;
        [ObservableProperty]
        private bool daFengJi_open_3;

        [ObservableProperty]
        private int daFengJi_status_4;
        [ObservableProperty]
        private bool daFengJi_open_4;


        [ObservableProperty]
        private int daFengJi_status_5;
        [ObservableProperty]
        private bool daFengJi_open_5;

        [ObservableProperty]
        private int cePaiFeng_status_1;
        [ObservableProperty]
        private bool cePaiFeng_open_1;

        [ObservableProperty]
        private int cePaiFeng_status_2;
        [ObservableProperty]
        private bool cePaiFeng_open_2;

        [ObservableProperty]
        private int cePaiFeng_status_3;
        [ObservableProperty]
        private bool cePaiFeng_open_3;

        [ObservableProperty]
        private int cePaiFeng_status_4;
        [ObservableProperty]
        private bool cePaiFeng_open_4;

        [ObservableProperty]
        private int cePaiFeng_status_5;
        [ObservableProperty]
        private bool cePaiFeng_open_5;

        [ObservableProperty]
        private int cePaiFeng_status_6;
        [ObservableProperty]
        private bool cePaiFeng_open_6;


        [ObservableProperty]
        private int xinFengJi_status_1;
        [ObservableProperty]
        private bool xinFengJi_open_1;
        [ObservableProperty]
        private int baiYeChuang_status_1;
        [ObservableProperty]
        private int baiYeChuang_open_1;
        [ObservableProperty]
        private int shuiBeng_status_1;
        [ObservableProperty]
        private int shuiBeng_open_1;
        [ObservableProperty]
        private int daoLiuBan_status_1;
        [ObservableProperty]
        private int daoLiuBan_open_1;  //DaoLiuBan_open_1






		[ObservableProperty]
        private int daoLiuBan_position_shang_1;
		[ObservableProperty]
		private int daoLiuBan_position_xia_1;
		[ObservableProperty]
		private string ip;
		[ObservableProperty]
		private int port;



        public ObservableCollection<UseModel> BaiYeChuangOpen { get; set; } = new ObservableCollection<UseModel>
        {
            new UseModel{ Name="关闭",Value=0},new UseModel{Name="30%",Value=1},new UseModel{Name="60%",Value=2},new UseModel{Name="100%",Value=3}
        };
		public ObservableCollection<UseModel> ShuiBengOpen { get; set; } = new ObservableCollection<UseModel>
		{
			new UseModel{ Name="关闭",Value=0},new UseModel{Name="50%",Value=1},new UseModel{Name="100%",Value=2}
		};
		public ObservableCollection<UseModel> DaoLiuBanOpen { get; set; } = new ObservableCollection<UseModel>
		{
			new UseModel{ Name="关闭",Value=0},new UseModel{Name="1档",Value=1},new UseModel{Name="2档",Value=2},new UseModel{Name="3档",Value=3}
		};


		public DeviceManagerViewModel()
        {
           
           
        }

        [RelayCommand]
        public void SaveDeviceManager()
        {
            var info = new DosingEquipment();
            this.UpdateTo(info);
            AppRuntime.GetService().GetService<DeviceManagerService>().SaveDeviceManager(info);
			Growl.SuccessGlobal("保存成功!");
            UpdateDevicePlc(info);

		}

        public void UpdateDevicePlc(DosingEquipment info)
        {
            PlcManager.GetQMitsubishiPlc(Region).UpdateDevicePage(info);
            AppRuntime.Logger_Info("保存设备参数并设置到PLC");
        }


		public void InitData()
        {
            if (string.IsNullOrWhiteSpace(Region)) return;
            var info = AppRuntime.GetService().GetService<DeviceManagerService>().GetDosingEquipment(Region);
            if(info == null) return;
            this.UpdateFrom(info);
          
            
        }






    }
}
